/**
 * 神州信息(2012)
 */
package net.abbp.watchkeeper;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import net.abbp.dao.DaoFactory;
import net.abbp.dao.IDao;

/**
 * 联系人输入卡片
 * 
 * @version 1.0
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ABBP 1.0
 * 
 *        Date：Apr 8, 2012
 */
public class JLinkmanCard extends JDialog {
    private JTextField txtUser;
    private JTextField txtMobile;
    private JComboBox cbxReceive;

    private JButton btnOK;
    private JButton btnCancel;
    private boolean isEdit = false;
    private int keyId;
    private IDao dao = DaoFactory.INSTANCE.newDao();
    private LinkmanPane mainPane;

    private static final Insets COMP_INSETS = new Insets(4, 4, 4, 4);

    public JLinkmanCard() {
        this.setAlwaysOnTop(true);

        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

        JPanel container = new JPanel();
        container.setBorder(BorderFactory.createEtchedBorder());
        container.setLayout(new GridBagLayout());
        container.add(new JLabel("联系人"), newGridBagConstraints(0, 0, 1, 1));
        this.txtUser = new JTextField(32);
        container.add(this.txtUser, newGridBagConstraints(1, 0, 1, 1));
        container.add(new JLabel("手机号码"), newGridBagConstraints(0, 1, 1, 1));
        this.txtMobile = new JTextField(32);
        container.add(this.txtMobile, newGridBagConstraints(1, 1, 1, 1));
        container.add(new JLabel("接收短信"), newGridBagConstraints(0, 2, 1, 1));
        this.cbxReceive = new JComboBox(new Object[] { "", "接收短信" });
        container.add(this.cbxReceive, newGridBagConstraints(1, 2, 1, 1));

        JPanel paneBtns = new JPanel();
        btnOK = new JButton("确定");
        btnOK.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                Linkman model = new Linkman();

                if (isEdit) {
                    model.setId(keyId);
                    updateModelByUI(model);
                    dao.update(model);
                    mainPane.refresh();
                } else {
                    updateModelByUI(model);
                    dao.insert(model);
                    mainPane.refresh();
                }

                JLinkmanCard.this.dispose();
            }
        });
        paneBtns.add(btnOK);
        btnCancel = new JButton("取消");
        btnCancel.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JLinkmanCard.this.dispose();
            }
        });
        paneBtns.add(btnCancel);
        container.add(paneBtns, newGridBagConstraints(0, 3, 2, 1));

        this.add(container);
        this.setTitle("联系人管理");
        this.setSize(new Dimension(310, 170));
        this.setPreferredSize(new Dimension(310, 170));
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation((screenSize.width - this.getWidth()) / 2, (screenSize.height - this.getHeight()) / 2);

        this.pack();
    }

    private GridBagConstraints newGridBagConstraints(int gridX, int gridY, int gridWidth, int gridHeight) {
        return new GridBagConstraints(gridX, gridY, gridWidth, gridHeight, 0, 0, GridBagConstraints.CENTER,
                GridBagConstraints.BOTH, COMP_INSETS, 0, 0);
    }

    /**
     * 根据主键ID更新
     * 
     * @param keyId
     *            主键Id
     */
    private void updateById(int keyId) {
        this.keyId = keyId;
        Linkman model = dao.fetch(Linkman.class, keyId);

        if (model != null) {
            this.updateUIByModel(model);
        }
    }

    /**
     * 根据模型更新界面
     * 
     * @param model
     *            模型
     */
    private void updateUIByModel(Linkman model) {
        this.txtUser.setText(model.getName());
        this.txtMobile.setText(model.getPhone());
        this.cbxReceive.setSelectedItem(model.getReceive());
    }

    /**
     * 根据界面更新模型
     * 
     * @param model
     *            模型
     */
    private void updateModelByUI(Linkman model) {
        model.setName(this.txtUser.getText().trim());
        model.setPhone(this.txtMobile.getText().trim());
        model.setReceive(this.cbxReceive.getSelectedItem().toString());
    }

    public static void edit(LinkmanPane linkmanPane, int keyId) {
        JLinkmanCard card = new JLinkmanCard();
        card.isEdit = true;
        card.updateById(keyId);
        card.mainPane = linkmanPane;

        card.setVisible(true);
    }

    public static void insert(LinkmanPane linkmanPane) {
        JLinkmanCard card = new JLinkmanCard();
        card.isEdit = false;
        card.mainPane = linkmanPane;

        card.setVisible(true);
    }

}
